﻿using System.IO;
using UnityEditor;
using UnityEngine;

namespace EditorExtension
{
    public class AssetDatabaseExample
    {
        private const string FolderPath = "Assets/EditorExtensions/5_ProjectExample/02_AssetDatabase/";
        private const string FolderName = "NewFolder";
        private const string NewFolderPath = FolderPath + FolderName;
        private const string NewMaterialPath = NewFolderPath + "/NewMaterial.mat";
        private const string MENU_PATH = "EditorExtensions/03_Project/01_AssetDatabase/";
        
        [MenuItem(MENU_PATH + "Create Material")]
        private static void CreateMaterial()
        {
            if (!Directory.Exists(NewFolderPath))
            {
                string guid = AssetDatabase.CreateFolder(FolderPath, FolderName);
                if (!string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(guid)))
                {
                    Debug.Log($"Folder Asset Created: {guid}");
                }
                else
                {
                    Debug.Log($"Folder Asset Create Fail!");
                }
            }
            
            var material = new Material(Shader.Find("Specular"));
            AssetDatabase.CreateAsset(material, NewMaterialPath);

            if (AssetDatabase.Contains(material))
            {
                Debug.Log($"Material Asset Created: {material.name}");
            }
            else
            {
                Debug.Log($"Material Asset Create Fail!");           
            }
        }
        
        [MenuItem(MENU_PATH + "Get Material GUID")]
        private static void GetMaterialGUID()
        {
            Debug.Log(AssetDatabase.AssetPathToGUID(NewMaterialPath));
        }
        
        [MenuItem(MENU_PATH + "Load Material")]
        private static void LoadMaterial()
        {
            Debug.Log(AssetDatabase.LoadAssetAtPath<Material>(NewMaterialPath));
        }
        
        [MenuItem(MENU_PATH + "Rename Material")]
        private static void RenameMaterial()
        {
            Debug.Log(AssetDatabase.RenameAsset(NewMaterialPath, "NewName"));;
        }
        
        [MenuItem(MENU_PATH + "Move Material")]
        private static void MoveMaterial()
        {
            Debug.Log(AssetDatabase.MoveAsset(NewMaterialPath, "Assets/Move.mat"));;
        }
        
        [MenuItem(MENU_PATH + "Copy Material")]
        private static void CopyMaterial()
        {
            Debug.Log(AssetDatabase.CopyAsset(NewMaterialPath, "Assets/copy.mat"));;
        }
        
        [MenuItem(MENU_PATH + "Delete Material")]
        private static void DeleteMaterial()
        {
            AssetDatabase.DeleteAsset(NewMaterialPath); // 或
            AssetDatabase.MoveAssetToTrash(NewMaterialPath);
            
            AssetDatabase.Refresh(); // 刷新
        }
        
        [MenuItem(MENU_PATH + "Get Material GUID", validate = true)]
        [MenuItem(MENU_PATH + "Load Material", validate = true)]
        [MenuItem(MENU_PATH + "Rename Material", validate = true)]
        [MenuItem(MENU_PATH + "Move Material", validate = true)]
        [MenuItem(MENU_PATH + "Copy Material", validate = true)]
        [MenuItem(MENU_PATH + "Delete Material", validate = true)]
        private static bool GetMaterialGUIDValidate()
        {
            return File.Exists(NewMaterialPath);
        }
        

    }
}